package com.aw.guiguzi.adapter

import android.widget.EditText
import android.widget.ImageView
import com.aw.baselib.util.AwDataUtil
import com.aw.baselib.util.AwEditTextListener
import com.aw.guiguzi.R
import com.aw.guiguzi.bean.NumItemBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder

class NumAdapter : BaseQuickAdapter<NumItemBean, BaseViewHolder>(R.layout.adapter_num) {
    private var mList: MutableList<NumItemBean>? = ArrayList()

    init {
        if (mList == null) {
            mList = ArrayList()
        }
    }

    override fun convert(helper: BaseViewHolder, bean: NumItemBean) {
        helper.setText(R.id.tv_nums, bean.name)
            .setText(
                R.id.et_silverNum,
                if (bean.getSilverCount() == 0) "" else bean.getSilverCount().toString()
            )
            .setText(
                R.id.et_skinNum,
                if (bean.getSkinCount() == 0) "" else bean.getSkinCount().toString()
            )
            .setText(
                R.id.et_gemstoneNum,
                if (bean.getGemstoneCount() == 0) "" else bean.getGemstoneCount().toString()
            )
            .addOnClickListener(R.id.cBox_status)
        val checkBox = helper.getView<ImageView>(R.id.cBox_status)
        if (bean.isChecked) {
            checkBox.background = mContext.resources.getDrawable(R.drawable.icon_checkbox_on)
        } else {
            checkBox.background = mContext.resources.getDrawable(R.drawable.icon_checkbox_off)
        }
        val et_silverNum = helper.getView<EditText>(R.id.et_silverNum)
        val et_skinNum = helper.getView<EditText>(R.id.et_skinNum)
        val et_gemstoneNum = helper.getView<EditText>(R.id.et_gemstoneNum)
        AwEditTextListener.isChangeListener(et_silverNum) { changed: Boolean ->
            if (changed) {
                if (AwDataUtil.getIntValue(et_silverNum.text.toString()) > 10) {
                    et_silverNum.setText("10")
                    bean.setSilverCount(10)
                }
            }
        }
        AwEditTextListener.isChangeListener(et_silverNum) { changed: Boolean ->
            if (changed) {
                if (AwDataUtil.getIntValue(et_skinNum.text.toString()) > 10) {
                    et_skinNum.setText("10")
                    bean.setSkinCount(10)
                }
            }
        }
        AwEditTextListener.isChangeListener(et_silverNum) { changed: Boolean ->
            if (changed) {
                if (AwDataUtil.getIntValue(et_gemstoneNum.text.toString()) > 200) {
                    et_gemstoneNum.setText("200")
                    bean.setSilverCount(200)
                }
            }
        }
    }

    fun addAllData(dataList: MutableList<NumItemBean>?) {
        mList = dataList
        setNewData(mList)
        notifyDataSetChanged()
    }

    fun clearData() {
        if (mList != null) {
            val startPosition = 0 //hasHeader is 1
            val preSize = mList!!.size
            if (preSize > 0) {
                mList!!.clear()
                notifyItemRangeRemoved(startPosition, preSize)
            }
        }
    }
}